package com.qf.dao;

import com.qf.entity.User;
import com.qf.util.ConnectionUtils;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * description: 用户数据的操作对象
 * author: Ken
 * 公众号：Java架构栈
 */
public class UserDao {

    /**
     * 注册用户
     * @return
     */
    public int insertUser(User user){
        //调用jdbc操作数据库
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //获得连接
            connection = ConnectionUtils.getConnection();

            String sql = "insert into user value(?,?,?,?,?,?,?)";
            System.out.println("需要执行的SQL：" + sql);
            statement = connection.prepareStatement(sql);
            //设置参数
            statement.setObject(1, user.getId());
            statement.setObject(2, user.getUsername());
            statement.setObject(3, user.getPassword());
            statement.setObject(4, user.getName());
            statement.setObject(5, user.getAge());
            statement.setObject(6, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getBirthday()));//java.util.Date 转成 java.sql.Date
            statement.setObject(7, user.getPowerStr());

            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            ConnectionUtils.close(connection, statement, null);
        }
        return 0;
    }

    /**
     * 根据用户名和密码查询用户信息
     * @param username
     * @param password
     * @return
     */
    public User queryUser(String username, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = ConnectionUtils.getConnection();

            String sql = "select * from user where username = ? and password = ?";
            statement = connection.prepareStatement(sql);
            //设置参数
            statement.setObject(1, username);
            statement.setObject(2, password);

            //执行查询
            resultSet = statement.executeQuery();
            //结果集的封装对象
            User user = new User();
            //循环获取数据
            if(resultSet.next()) {
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setName(resultSet.getString("name"));
                user.setAge(resultSet.getInt("age"));
                user.setBirthday(resultSet.getTimestamp("birthday"));

                //反向处理权限
                //1-2-3 -> List<String>
                String powers = resultSet.getString("powers");
                List<String> powersList = Arrays.asList(powers.split("-"));
                user.setPowers(powersList);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.close(connection, statement, resultSet);
        }
        return null;
    }

    /**
     * 根据id 修改用户信息
     * @return
     */
    public int updateById(User user){
        Connection connection = ConnectionUtils.getConnection();
        PreparedStatement ps = null;
        //
        String sql = "update user set username=?,password=?,name=?,age=?,birthday=?,powers=? where id=?";
        try {
            ps = connection.prepareStatement(sql);
            //设置参数
            ps.setObject(1, user.getUsername());
            ps.setObject(2, user.getPassword());
            ps.setObject(3, user.getName());
            ps.setObject(4, user.getAge());
            ps.setObject(5, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getBirthday()));
            ps.setObject(6, user.getPowerStr());
            ps.setObject(7, user.getId());

            //执行修改
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.close(connection, ps, null);
        }
        return -1;
    }
}
